Imports System.Data

Public MustInherit Class BOBase

    Protected m_TableIndex As Integer = 0
    Protected m_RowIndex As Integer = 0

    Sub New()

    End Sub

    Sub New(ByRef stateIn As DataSet)
        Me.ObjectState = stateIn
    End Sub

#Region "   Properties "
    Public ReadOnly Property TableIndex() As Integer
        Get
            Return m_TableIndex
        End Get
    End Property

    Public ReadOnly Property RowIndex() As Integer
        Get
            Return m_RowIndex
        End Get
    End Property

    Public MustOverride ReadOnly Property DA() As DABase

    Public MustOverride Property ObjectState() As DataSet

    Public ReadOnly Property Table() As DataTable
        Get
            Return ObjectState.Tables(TableIndex)
        End Get
    End Property

    Public ReadOnly Property Row() As DataRow
        Get
            Return Me.Table.Rows(RowIndex)
        End Get
    End Property

#End Region

#Region "   Methods "
    Public Sub ImportRow(ByVal row As DataRow)
        Try
            ObjectState.Tables(TableIndex).Rows.Add(row)
        Catch ex As Exception
            Try
                Dim dr As DataRow = ObjectState.Tables(TableIndex).NewRow
                dr.ItemArray = row.ItemArray
                ObjectState.Tables(TableIndex).Rows.Add(dr)
            Catch ex2 As Exception
                Throw New ApplicationException("Invalid Data Row.", ex)
            End Try
        End Try
    End Sub

    Public MustOverride Sub Init()

    Public Overridable Overloads Sub Load(ByVal keys() As Object)
        Me.ObjectState.Tables(TableIndex).Clear()
        Me.DA.Select(keys, ObjectState, ObjectState.Tables(TableIndex).TableName)
        If Me.ObjectState.Tables(TableIndex).Rows.Count > 1 Then
            Throw New ApplicationException("Object loaded with more  than one data entry!", Nothing)
        ElseIf Me.ObjectState.Tables(TableIndex).Rows.Count < 1 Then
            Throw New DataNotExistsException("Data does not exist!", Nothing)
        End If
    End Sub

    Public Overloads Sub Load(ByVal key As Object)
        Dim keys(0) As Object
        keys(0) = key
        Me.Load(keys)
    End Sub

    Public Overridable Sub Save()
        Me.DA.Update(Me.ObjectState, Table.TableName)
    End Sub

    Public Sub Save(ByVal connection As SqlClient.SqlConnection)
        Me.DA.Connection = connection
        Me.DA.Update(Me.ObjectState, Table.TableName)
    End Sub

    Public Overridable Sub Save(ByVal transaction As SqlClient.SqlTransaction)
        Me.DA.Transaction = transaction
        Me.DA.Connection = transaction.Connection
        Me.DA.Update(Me.ObjectState, Table.TableName)
    End Sub

    Public Overridable Overloads Sub Remove()
        Me.ObjectState.Tables(TableIndex).Rows(RowIndex).Delete()
        Me.DA.Update(Me.ObjectState, ObjectState.Tables(TableIndex).TableName)
    End Sub

    Public Overridable Overloads Sub Remove(ByVal transaction As SqlClient.SqlTransaction)
        Me.ObjectState.Tables(TableIndex).Rows(RowIndex).Delete()
        With Me.DA
            Me.DA.Transaction = transaction
            Me.DA.Connection = transaction.Connection
            .Update(Me.ObjectState, ObjectState.Tables(TableIndex).TableName)
        End With
    End Sub
#End Region

End Class
